package mplayer.dao;

import mplayer.entity.Text;
import mplayer.entity.User;

import org.hibernate.Session;
import org.hibernate.Transaction;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;

public class TextService {
	Long resultsCount;
	
	
	public List<Text> getText(Integer maxResults,Integer firstResult) {
		List<Text> list ;
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		Query query = session.createQuery("from Text");
		resultsCount = (Long) session.createCriteria(User.class).setProjection(Projections.rowCount()).uniqueResult();
		query.setMaxResults(maxResults);
		query.setFirstResult(firstResult);
		list = query.list();
		tx.commit();
		session.close();		
		return list;
	}
	
	public void SaveText(Text text) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		session.save(text);
		tx.commit();
		session.close();
		 
	}
	
	
	public Long getResultsCount() {
//		Session session = HibernateUtil.getSessionFactory().openSession();
//		Transaction tx = session.beginTransaction();
//		Query query = session.createQuery("from Text");
//		resultsCount = (Long) session.createCriteria(Text.class).setProjection(Projections.rowCount()).uniqueResult();
//		tx.commit();
//		session.close();
		return resultsCount;
	}

	public void setResultsCount(Long resultsCount) {
		this.resultsCount = resultsCount;
	}
	
}
